import axios from "axios";

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.timeout = 30000
axios.defaults.withCredentials = true
axios.defaults.baseURL = 'http://localhost:8888'

axios.interceptors.request.use(  
    config => {  
        // 从本地存储获取 token  
        const token = localStorage.getItem('token'); 
        if (token) {  
            // 添加 Authorization 头  
            config.headers['Authorization'] = `Bearer ${token}`;  
        }  
        // 添加跨域相关的请求头  
        config.headers['Access-Control-Allow-Origin'] = 'http://localhost:8080';  
        config.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE';  
        config.headers['Access-Control-Allow-Headers'] = 'content-type';  
        return config;  
    },  
    error => {  
        console.log('request error');  
        return Promise.reject(error);  
    }  
);

axios.interceptors.response.use(
    (response) => {
        return response;
    },
    error =>{
        return Promise.reject(error);
    }
)

export function request(url,config = {}){
    return axios.get(url,config);
}

export default axios